package com.ewzj.bigdata.mvp.presenter

import android.app.Application
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.ewzj.bigdata.R
import com.ewzj.bigdata.app.config.AppKey
import com.ewzj.bigdata.app.utils.RxUtils
import com.ewzj.bigdata.mvp.contract.CitySelectorActivityContract
import com.ewzj.bigdata.mvp.contract.MessageActivityContract
import com.ewzj.bigdata.mvp.model.bean.CitygroupBean
import com.ewzj.bigdata.mvp.model.reqbean.ReqCityBean
import com.ewzj.bigdata.mvp.model.reqbean.ReqSearchCityBean
import com.ewzj.bigdata.mvp.model.reqbean.Reqpagebean
import com.ewzj.bigdata.mvp.model.rspbean.*
import com.ewzj.bigdata.mvp.ui.adapter.CityAdapter
import com.ewzj.bigdata.mvp.ui.adapter.HotcityAdapter
import com.ewzj.bigdata.mvp.ui.adapter.MsgAdapter
import com.ewzj.bigdata.mvp.ui.view.ObservableRecyclerView
import com.google.gson.Gson
import com.jess.arms.mvp.BasePresenter
import com.jess.arms.utils.DataHelper
import com.jess.arms.utils.DeviceUtils
import com.jess.arms.utils.UiUtils
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.observables.GroupedObservable
import io.reactivex.schedulers.Schedulers
import me.jessyan.rxerrorhandler.core.RxErrorHandler
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber
import java.util.*
import javax.inject.Inject

/**
 * author:zhangjing
 * 作用:
 * return:
 */
class MessageActivityPresenter @Inject constructor(val mGson: Gson, val mErrorHandler: RxErrorHandler, val application: Application, view: MessageActivityContract.View, model: MessageActivityContract.Model) : BasePresenter<MessageActivityContract.Model, MessageActivityContract.View>(model, view) {
    val datas= arrayListOf<MsgBean>()
    val adapter=MsgAdapter(datas)
    val context=mRootView as AppCompatActivity
    var  page=1
    fun loadFirstPageData(){
        page=1
        mModel.loadMsgData(Reqpagebean(1)).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(object :ErrorHandleSubscriber<BaseData<BaseListData<MsgBean>>>(mErrorHandler){
                    override fun onNext(t: BaseData<BaseListData<MsgBean>>) {
                        page++
                        datas.clear()
                        datas.addAll(t.data!!.list)
                        if (adapter.emptyView==null){
                            adapter.emptyView=UiUtils.inflate(context, R.layout.error_view)
                        }
                        adapter.setNewData(datas)
                        if (t.data!!.list.size<AppKey.PAGE_NUM){
                            adapter.disableLoadMoreIfNotFullPage()
                        }
                    }

                })
    }
    fun initAdapter(recyclerView: RecyclerView){
        recyclerView.layoutManager=LinearLayoutManager(context)
        recyclerView.adapter=adapter
        adapter.setOnLoadMoreListener(object :BaseQuickAdapter.RequestLoadMoreListener{
            override fun onLoadMoreRequested() {
                loadNextPageData()
            }

        },recyclerView)
        //        adapter.disableLoadMoreIfNotFullPage()
    }
    fun loadNextPageData(){
        mModel.loadMsgData(Reqpagebean(page)).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .doAfterTerminate { if (adapter.isLoading){
                    adapter.loadMoreComplete()
                } }.subscribe({t ->
            run{
                page++
                adapter.addData(t.data!!.list)
                if (t.data!!.list.size<AppKey.PAGE_NUM){
                    adapter.loadMoreEnd()
                }
            }

        },{adapter.loadMoreFail()})
    }
}